# -*- coding: utf-8 -*-

import unittest
import os.path
from wolfox.foxit.base.tutils import *

class ModuleTest(unittest.TestCase):
    def testTempFile(self): #测试mktemp,removetemp
        fo = mktemp('test code')
        self.assertEquals('test code',open(fo['name']).read())
        removetemp(fo)
        self.assertRaises(IOError,open,fo['name'])
    
    def testParseName(self):
        self.assertEquals("zzz",parseName("c:/xxx/yyy/zzz.txt"))
        self.assertEquals("zzz",parseName("c:/zzz.txt"))
        self.assertEquals("zzz",parseName("xxx/yyy/zzz.txt"))
        self.assertEquals("zzz",parseName("zzz.txt"))
        self.assertEquals("zzz",parseName("zzz"))

    def testLoggingConfig(self):    #通路测试,
        import logging
        logging_config('a.log','b.log')
        logging_config('a.log','b.log',logging.ERROR,logging.INFO)
        logging_config('c.log')
        logging_config('at.log','s.log')
        logger = logging.getLogger('tuilts.test')
        logger.debug('debug msg')
        logger.info('info msg')
        logger.warning('warning msg')
        self.assertTrue(True)
        #logging.shutdown()  #关闭日志系统之后才能删除文件,但不能这么暴力，会把外围测试驱动者的logging也关掉
        logger = logging.getLogger()
        for handler in log_handlers:
            logger.removeHandler(handler)
            handler.close()
        os.remove('a.log')
        os.remove('b.log')
        os.remove('c.log')
        os.remove('at.log')
        os.remove('s.log')
    
    def testLinelog(self):
        linelog('test line log')
        self.assertTrue(True)

    def testWriteRecord(self):
        fo = mktemp('')
        fh = open(fo['name'],'w')
        writerecord(fh,(1,2,3,4,5))
        fh.close()
        removetemp(fo)

    def testWriteRecords(self):
        fo = mktemp('')
        fh = open(fo['name'],'w')
        writerecords(fh,(1,2,3,4,5),(3,4,5,6,7),('8a','a1',2,3,4))
        fh.close()
        removetemp(fo)


if __name__ == "__main__":
    unittest.main()

